/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.core.action;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.grayrabbit.pay.chinapnr.common.ChinapnrGate;
import org.grayrabbit.pay.facade.ChinapnrClient;
import org.grayrabbit.pay.facade.bean.ChinapnrTrade;
import org.grayrabbit.pay.facade.bean.Money;
import org.grayrabbit.pay.facade.common.Result;
import org.grayrabbit.pay.facade.common.TradeConstants.TradeCode;
import org.grayrabbit.web.struts2.GenericAction;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * @author wy
 * @version $Id: FrameAction.java, v 0.1 2012-6-11 上午12:20:22 wy Exp $
 */
/**
 * 汇付支付
 * 
 * @author wy
 * @version $Id: FrameAction.java, v 0.1 2012-6-11 上午12:22:09 wy Exp $
 */
@InterceptorRefs({@InterceptorRef("defaultStack") })
public class ChinaPnrAction extends GenericAction {

    /** serialVersionUID */
    private static final long serialVersionUID = 3420110288553311653L;

    @Autowired
    private ChinapnrClient    chinapnrClient;

    /**订单编号*/
    private String            orderId;
    
    /**
     * Get value from orderId
     *
     * @return orderId
     */
    public String getOrderId() {
        return orderId;
    }

    /**
     * Set property for orderId
     * 
     * @param orderId
     */
    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    @Action(value = "chinanr_pay")
    public String pay() {
        ChinapnrTrade chinapnrTrade = new ChinapnrTrade();
        String systemCode = getRequest().getParameter("systemCode");
        String orderId = getRequest().getParameter("orderId");
        String name = getRequest().getParameter("name");
        String money = getRequest().getParameter("money");
        String gate = getRequest().getParameter("gate");
        String cardNo = getRequest().getParameter("cardNo");
        String validDate = getRequest().getParameter("validDate");
        String cvv2 = getRequest().getParameter("cvv2");
        String idName = getRequest().getParameter("idName");
        String idNo = getRequest().getParameter("idNo");
        chinapnrTrade.setSystemCode(systemCode);
        chinapnrTrade.setOrderId(orderId);
        chinapnrTrade.setName(name);
        chinapnrTrade.setMoney(new Money().convertFen((long) (Double.valueOf(money) * 100)));
        chinapnrTrade.setGate(gate);
        chinapnrTrade.setCardNo(cardNo);
        chinapnrTrade.setValidDate(validDate);
        chinapnrTrade.setCvv2(cvv2);
        chinapnrTrade.setIdName(idName);
        chinapnrTrade.setIdNo(idNo);

        Result<TradeCode> result = chinapnrClient.getChinapnrPay(chinapnrTrade);
        JSONObject json = new JSONObject();
        if (result.getIsSuccess()) {
            json.put(STATUS, true);
            json.put(MESSAGE, result.getData().getMsg());
            return renderJson(json.toString());
        } else {
            json.put(STATUS, true);
            json.put(MESSAGE, result.getResultCode() + ":" + result.getResultMsg());
            return renderJson(json.toString());
        }
    }

    @Action(value = "chinapnr_input")
    public String input() {
        return INPUT;
    }

    @Action(value = "chinapnr_query")
    public String query() {

        return INPUT;
    }

    public Map<String, String> getGateMap() {
        ChinapnrGate[] gates = ChinapnrGate.values();
        Map<String, String> map = new HashMap<String, String>();
        for (ChinapnrGate gate : gates) {
            map.put(gate.getGate(), gate.getBank());
        }
        return map;
    }

}
